package com.ky.user.strategy;

import cn.hutool.core.util.RandomUtil;

import com.ky.common.config.RedisKeyConfig;
import com.ky.common.param.MqMsg;
import com.ky.user.entity.SmsLog;
import com.ky.user.mapper.SmsLogMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * --- 天道酬勤 ---
 *
 * @author 1379
 * @date 2024/6/5
 * @desc -- 代码敲烂 月薪过万--
 */
@Service
public class RegistryCodeStrategy implements AbstractStrategy{

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SmsLogMapper smsLogMapper;

    @Override
    public void doMqMsg(MqMsg mqMsg) {
        System.out.println("消费者得到消息 msg = " + mqMsg);

        String code = RandomUtil.randomInt(1000, 9999)+"";
        String phone = (String) mqMsg.getMsg();
        System.out.println("phone = " + phone + "---验证吗 = "+code);

        // boolean result = AliSmsUtil.sendCode(phone, code);

        stringRedisTemplate.opsForValue().set(RedisKeyConfig.SMS_RCODE+phone,code,RedisKeyConfig.SMS_RCODE_TIME, TimeUnit.SECONDS);
        smsLogMapper.insertSmsLog(new SmsLog(phone,"注册验证码",phone+"注册验证码: "+code, new Date()));
    }

    @Override
    public int getType() {
        return 1;
    }
}
